package huawei;

/**
 * 第n个水仙花数
 */
public class Q114 {

    public static void main(String[] args) {
        int N = 3;
        int M = 4;

        if (N < 3 || N > 7) {
            System.out.println(-1);
        }

        int min = (int) Math.pow(10, N-1);
//        System.out.println(min);
        int max = (int) (Math.pow(10, N) - 1);
//        System.out.println(max);


        int count = 0;
        int maxNum = 0;
        boolean find = false;
        for (int num = min; num <= max; num++) {
            int n = num;

            int sum = 0;

            while (n / 10 != 0) {
//                System.out.println(n % 10);
                sum += (int) Math.pow(n % 10, N);
                n /= 10;
            }
            sum += (int) Math.pow(n, N);
            if (sum == num) {
                if (count++ == M) {
                    System.out.println(num);
                    find = true;
                    break;
                } else {
                    maxNum = num;
                }
            }
        }
        if (!find) {
            System.out.println(maxNum);
        }
    }
}
